#!/usr/bin/perl -wT

###########
# MODULES #
###########

use CGI::Pretty qw/:standard/;
use DBI;
use strict;

#############
# CONSTANTS #
#############

use constant PARAMS   => '';
use constant USER     => '';
use constant PASSWORD => '';

###########
# GLOBALS #
###########

my $run_mode = param('run_mode');
my $dbh = DBI->connect (PARAMS, USER, PASSWORD);

########
# MAIN #
########
	
print header( 'text/html' ),
	  start_html( {-title => "DFI CAFLM Website",
				   -style => {-src=> '/css/index.css'}
				}
	  );

if ($run_mode eq 'request_partnership') {
	request_partnership();
}
elsif ($run_mode eq 'upload_resources') {
	upload_resources();
}
elsif ($run_mode eq 'register') {
	register();
}
elsif ($run_mode eq 'show_control_panel') {
	show_control_panel();
}
else {
	show_error();
}

########
# SUBS #
########

sub request_partnership {

}

sub upload_resources {

}

sub register {

}

sub show_control_panel {

}

# Displays an error in the case that the run mode that
# was passed into this program is invalid.
sub show_error {
	print "Something went wrong...<br />";
	print "The run mode was: ", $run_mode;
	print end_html();
}

# Checks to see if the given email contains characters
# before and after the @ symbol.
sub valid_email {
	my $email = shift;
	my $sql   = "";
	
	if ($email =~ m/.+@.+/) {
		return 1;
	}
	else {
		return 0;
	}
}

# Checks to see if the given phone number is in an
# acceptable format.
sub valid_phone {
	(my $phone) = @_;
	
	if ($phone eq '') {
		return 1;
	}
	elsif (($phone =~ m/^\(\d{3}\) \d{3}-\d{4}$/) ||
		  ($phone =~ m/^\(\d{3}\)\d{3}-\d{4}$/) ||
		  ($phone =~ m/^\d{3}\.\d{3}\.\d{4}$/) ||
		  ($phone =~ m/^\d{3}-\d{3}-\d{4}$/)) {
		return 1;
	}
	else {
		return 0;
	}
}

# Returns whether the first and last name were filled out.
sub requirements_met {
	my $first_name = shift;
	my $last_name  = shift;
	
	if (($first_name ne '') && ($last_name ne '')) {
		return 1;
	}
	else {
		return 0;
	}
}

# Prints invalid or missing information message.
sub print_error_message {
	my $first_name = shift;
	my $last_name  = shift;
	my $email      = shift;
	my $phone      = shift;
	my $old_email  = shift;
	my $sql        = "";

	print "Could not submit the data for the following reasons:<br />";
	
	if ($first_name eq '') {
		print "*First name required.<br />";
	}
	if ($last_name eq '') {
		print "*Last name required.<br />";
	}
	if ($email eq '') {
		print "*Email address required.<br />";
	}
	if (($email ne '') && !(valid_email($email))) {
		print "*Email address is not valid.<br />";
	}
	if (($email ne '') && (valid_email($email)) && (duplicate_email($email)) && ($email ne $old_email)) {
		print "*Email address is already used.<br />";
	}
	if (($phone ne '') && !(valid_phone($phone))) {
		print "*Phone number is not valid.<br />";
	}
}

# Checks to see if the given email is already used in the database.
sub duplicate_email {
	my $email = shift;
	my $sql   = "";
	my $query;
	
	$sql   = 'SELECT * FROM users';
	$query = $dbh->prepare($sql);
	$query->execute();
	while (my @row = $query->fetchrow_array()) {
		if ($row[2] eq $email) {
			return 1;
		}
	}
}